package fr.inserm.um915.bdibd.model;

import com.sleepycat.bind.EntryBinding;
import com.sleepycat.je.DatabaseEntry;

@Deprecated
public class RsBinding implements EntryBinding<String>
	{
	private static final RsBinding INSTANCE=new RsBinding();
	
	private RsBinding()
	{
		
	}
	
	@Override
	public String entryToObject(DatabaseEntry e)
		{
		byte b[]=e.getData();
		int id=   (b[0] << 24)
		        + ((b[1] & 0xFF) << 16)
		        + ((b[2] & 0xFF) << 8)
		        + (b[3] & 0xFF);
		return "rs"+id;
		}
	@Override
	public void objectToEntry(String rs, DatabaseEntry entry)
		{
		if(!rs.startsWith("rs")) throw new IllegalArgumentException(rs);
		int value=Integer.parseInt(rs.substring(2));
		entry.setData(new byte[] {
                (byte)(value >>> 24),
                (byte)(value >>> 16),
                (byte)(value >>> 8),
                (byte)value}
				);
		}
	public static void main(String[] args) throws Exception
		{
		DatabaseEntry e=new DatabaseEntry();
		java.util.Random rand=new java.util.Random();
	
		for(int j=0;j<= 1000000;++j)
			{
			String rs="rs"+rand.nextInt();
			INSTANCE.objectToEntry(rs, e);
			String rs2=INSTANCE.entryToObject(e);
			if(!rs.equals(rs2)) throw new IllegalArgumentException(rs+" "+rs2);
			}
		System.out.println("Done.");
		}
	}
